﻿#include "CChestBlockActor.h"

#include "../Utils/Utils.h"

bool C_ChestBlockActor::isPaired() const {
	return *reinterpret_cast<int64_t*>(reinterpret_cast<uintptr_t>(this) + 0x220) != 0;  // ptr to paired chest block actor
}

vec3_ti& C_ChestBlockActor::getPairedPos() const {
	return *reinterpret_cast<vec3_ti*>(reinterpret_cast<uintptr_t>(this) + 0x228);
}

bool C_ChestBlockActor::isMainSubchest() const {
	return static_cast<bool>(*reinterpret_cast<uint8_t*>(reinterpret_cast<uintptr_t>(this) + 0x1FC) & 1ui8);  // in paired chest, is it the main one
}

AABB C_ChestBlockActor::getFullAABB() {
	if (!isPaired()) {
		// if (isBarrelBlock() || isShulkerBlock())
		// return AABB(this->posI.toVec3t(), this->posI.toVec3t().add(1.f));
		return AABB{add(toVec3t(this->posI), 0.0625f, 0.f, 0.0625f), add(toVec3t(this->posI), 1.f - 0.0625f, 1.f - 1.f / 8.f, 1.f - 0.0625f)};
	}
	vec3_ti first = this->posI;
	vec3_ti second = getPairedPos();

	if (first.x > second.x || first.z > second.z)
		std::swap(first, second);

	return AABB{add(toVec3t(first), 0.0625f, 0.f, 0.0625f), add(toVec3t(second), 1.f - 0.0625f, 1.f - 1.f / 8.f, 1.f - 0.0625f)};
}

// bool C_ChestBlockActor::isBarrelBlock() {
//	GamerTextHolder alloc;
//	Utils::Utils::CallVFunc<25, void, GamerTextHolder*, int64_t>(this, &alloc, 0i64);
//	return strcmp((char*)alloc.getText(), "container.barrel") == 0;
// }
//
// bool C_ChestBlockActor::isShulkerBlock() {
//	GamerTextHolder alloc;
//	Utils::Utils::CallVFunc<25, void, GamerTextHolder*, int64_t>(this, &alloc, 0i64);
//	return strcmp((char*)alloc.getText(), "container.shulker") == 0;
// }

AABB C_ChestBlockActor::getObstructionAABB() {
	constexpr uint8_t coolPtr[sizeof(AABB) + 4]{};
	Utils::Utils::CallVFunc<40, void, void*>(this, (void*)coolPtr);
	AABB ret = *reinterpret_cast<AABB*>((void*)coolPtr);
	return ret;
}